package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmPushPageListDto;
import com.kaili.basic.pojo.entity.MdmPushEntity;
import com.kaili.basic.pojo.vo.MdmPushPageListVo;
import com.kaili.basic.pojo.vo.MdmPushVo;
import com.kaili.basic.service.IMdmPushSerivce;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.DateUtil;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Desc:   推送管理Controller
 * @author: jianfeng
 * @Time: 2019/7/31 10:37
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "推送管理" ,description = "推送管理相关API")
public class MdmPushController {

    @Autowired
    private IMdmPushSerivce pushSerivce;

    @ApiOperation("新增推送")
    @PostMapping("/push")
    public RestStatus add(@RequestBody MdmPushEntity entity){
        pushSerivce.add(entity);
        return CommonStatus.ADD_OK;
    }

    @ApiOperation("推送管理分页")
    @GetMapping("/push")
    public Page<MdmPushPageListVo> queryPageList(@ModelAttribute MdmPushPageListDto pushPageListDto){
        return pushSerivce.queryPageList(new Page<>(pushPageListDto.getPage(),pushPageListDto.getPageSize()),pushPageListDto);
    }

    @ApiOperation("删除推送信息（逻辑删除）")
    @DeleteMapping("/push/{id}")
    public RestStatus delete(@PathVariable Integer id){
        MdmPushEntity entity = new MdmPushEntity();
        entity.setId(id);
        entity.setRdStatus("0");
        pushSerivce.updateById(entity);
        return CommonStatus.DELETE_OK;
    }

    @ApiOperation("导出")
    @GetMapping("/push/export")
    public void export(HttpServletResponse response, @ModelAttribute MdmPushPageListDto pushPageListDto) throws RestException {
        List<MdmPushPageListVo> list=pushSerivce.export(pushPageListDto);
        ExcelUtil.exportExcel(list,"推送管理","sheet1", MdmPushPageListVo.class,"策略管理"+ DateUtil.getNowDate()+".xls",response);
    }

    @PutMapping(value = "/push")
    @ApiOperation("修改推送信息")
    public RestStatus modify(@RequestBody MdmPushEntity entity) {
        pushSerivce.modify(entity);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping(value = "/push/{id}")
    @ApiOperation("根据ID查询推送管理")
    public MdmPushVo queryById(@ApiParam("ID") @PathVariable("id") Integer id) {
        return pushSerivce.queryById(id);
    }
}
